package com.wuhao.springboot.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wuhao.springboot.common.Result;
import com.wuhao.springboot.entity.Part;
import com.wuhao.springboot.entity.ro.PartPageRO;
import com.wuhao.springboot.service.PartService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @Description
 * @Date 2023/1/30 23:22
 * @Author pq
 */
@RestController
@RequestMapping("/part")
public class PartController {

    @Resource
    private PartService partService;

    /**
     * @Author pq
     * @Description partImg 必须跟前端formData的key一致
     * @Date 1:32 2023/1/31
     * @Param [file, request]
     * @return com.wuhao.springboot.common.Result
     **/
    @PostMapping("/uploadImg")
    public Result uploadImg(@RequestParam("partImg") MultipartFile file, HttpServletRequest request){
        System.out.println("上传零件图片");
        return Result.success(partService.uploadImg(file));
    }

    @PostMapping("/addPart")
    public Result addPart(@RequestBody Part part){
        partService.addPart(part);
        return Result.success();
    }

    @PostMapping("/getPartByPage")
    public IPage<Part> getPartByPage(@RequestBody PartPageRO ro){
        return partService.getPartByPage(ro.getCurrent(),ro.getSize());
    }

    @PostMapping("/delete")
    public Result getPartByPage(@RequestBody Part part){
        partService.delete(part.getId());
        return Result.success();
    }

    @PostMapping("/update")
    public Result update(@RequestBody Part part){
        partService.update(part);
        return Result.success();
    }
}
